/**
 * 
 */
package denaris.core.controller;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import denaris.factory.ImageFactory;
import denaris.midlet.GameScreen;
import denaris.midlet.DenarisGameCanvas.DisplayProperties;

/**
 * @author stkl23
 *
 */
public class LooseSatelliteController extends AbstractController {
	private static final Image SATELLITEIMAGE = ImageFactory.getImage("/denaris/resource/sprite/satellite0.png");
	private static final int MIDDLE = DisplayProperties.DISPLAY_WIDTH >> 1;
	
	private int delay = 0;
	
	public LooseSatelliteController() {
		super();
		setSprite(new Sprite(SATELLITEIMAGE, 18, 16));
	}
	
	/* (non-Javadoc)
	 * @see denaris.core.controller.AbstractController#advance()
	 */
	public boolean advance() {
		int dy = 0;
		if (delay <= 0) {
			int x = controllable.getX();
			// move the controllable into the desired direction
			dy = x < MIDDLE ? -1 : 1;
			// calculate the new delay
			delay = x - MIDDLE;
			if (delay < 0) {
				delay *= -1;
			}
			delay = MIDDLE / delay;
		}else {
			delay--;
		}
		
		controllable.move(-GameScreen.SCROLL_SPEED * 3, dy);
		
		if (controllable.getX() < -20) {
			return false;
		}else {
			return true;
		}
	}
}
